import { MockMethod } from 'vite-plugin-mock'
import { Message, MessageSettings } from '@/types/message'
import { PaginationData } from '@/types'

// 模拟消息数据
const messages: Message[] = [
  {
    id: 1,
    title: '系统升级通知',
    content: '系统将于今晚24:00进行升级维护，预计持续2小时，请提前做好准备。',
    time: '2025-06-26 10:30:00',
    type: 'system',
    read: false
  },
  {
    id: 2,
    title: '新用户注册提醒',
    content: '今日新增注册用户10名，请及时查看并审核。',
    time: '2025-06-26 09:15:00',
    type: 'info',
    read: true
  },
  {
    id: 3,
    title: '安全警告',
    content: '检测到异常登录行为，IP地址: 192.168.1.1，请确认是否为您本人操作。',
    time: '2025-06-25 18:45:00',
    type: 'alert',
    read: false
  },
  {
    id: 4,
    title: '待审核内容提醒',
    content: '有5篇新文章等待审核，请尽快处理。',
    time: '2025-06-25 15:30:00',
    type: 'task',
    read: false
  },
  {
    id: 5,
    title: '服务器状态预警',
    content: '服务器CPU使用率超过90%，请检查系统运行状态。',
    time: '2025-06-25 14:20:00',
    type: 'alert',
    read: true
  },
  {
    id: 6,
    title: '系统功能更新',
    content: '新增数据分析模块，提供更全面的数据统计和可视化功能。',
    time: '2025-06-24 11:00:00',
    type: 'system',
    read: false
  },
  {
    id: 7,
    title: '账户信息变更',
    content: '您的账户权限已更新，现在可以访问更多系统功能。',
    time: '2025-06-24 09:30:00',
    type: 'info',
    read: true
  },
  {
    id: 8,
    title: '定时任务完成',
    content: '数据库备份任务已成功完成，备份文件已保存至指定位置。',
    time: '2025-06-23 23:00:00',
    type: 'task',
    read: false
  },
  {
    id: 9,
    title: '用户反馈通知',
    content: '收到3条新的用户反馈，请及时查看并回复。',
    time: '2025-06-23 16:45:00',
    type: 'task',
    read: false
  },
  {
    id: 10,
    title: '系统公告',
    content: '端午节放假通知：6月10日至12日放假3天，13日正常上班。',
    time: '2025-06-23 10:15:00',
    type: 'system',
    read: true
  },
  {
    id: 11,
    title: '安全漏洞修复',
    content: '系统已完成安全漏洞修复，请更新至最新版本以确保系统安全。',
    time: '2025-06-22 14:30:00',
    type: 'alert',
    read: false
  },
  {
    id: 12,
    title: '存储空间警告',
    content: '系统存储空间已使用85%，请及时清理不必要的文件。',
    time: '2025-06-22 11:20:00',
    type: 'alert',
    read: true
  }
]

// 消息设置
let messageSettings: MessageSettings = {
  system: true,
  task: true,
  alert: true,
  desktop: false,
  sound: false
}

export default [
  // 获取消息列表
  {
    url: '/api/messages',
    method: 'get',
    response: (request: any) => {
      const { page = 1, pageSize = 10, type = 'all' } = request.query
      
      let filteredMessages = [...messages]
      
      // 根据类型过滤
      if (type !== 'all') {
        if (type === 'unread') {
          filteredMessages = filteredMessages.filter(msg => !msg.read)
        } else {
          filteredMessages = filteredMessages.filter(msg => msg.type === type)
        }
      }
      
      // 按时间排序（最新的在前面）
      filteredMessages.sort((a, b) => new Date(b.time).getTime() - new Date(a.time).getTime())
      
      // 分页
      const startIndex = (page - 1) * pageSize
      const endIndex = startIndex + pageSize
      const pageMessages = filteredMessages.slice(startIndex, endIndex)
      
      return {
        code: 200,
        message: 'success',
        data: {
          list: pageMessages,
          total: filteredMessages.length,
          page: Number(page),
          pageSize: Number(pageSize)
        } as PaginationData<Message>
      }
    }
  },
  
  // 获取未读消息数量
  {
    url: '/api/messages/unread/count',
    method: 'get',
    response: () => {
      const unreadCount = messages.filter(msg => !msg.read).length
      
      return {
        code: 200,
        message: 'success',
        data: unreadCount
      }
    }
  },
  
  // 获取消息详情
  {
    url: '/api/messages/:id',
    method: 'get',
    response: (request: any) => {
      const id = request.params?.id || request.query?.id
      if (!id) {
        return {
          code: 400,
          message: '缺少消息ID参数',
          data: null
        }
      }
      
      const message = messages.find(msg => msg.id === Number(id))
      
      if (!message) {
        return {
          code: 404,
          message: '消息不存在',
          data: null
        }
      }
      
      return {
        code: 200,
        message: 'success',
        data: message
      }
    }
  },
  
  // 标记消息为已读
  {
    url: '/api/messages/:id/read',
    method: 'patch',
    response: (request: any) => {
      const id = request.params?.id || request.query?.id
      if (!id) {
        return {
          code: 400,
          message: '缺少消息ID参数',
          data: null
        }
      }
      
      const message = messages.find(msg => msg.id === Number(id))
      
      if (!message) {
        return {
          code: 404,
          message: '消息不存在',
          data: null
        }
      }
      
      message.read = true
      
      return {
        code: 200,
        message: '已标记为已读',
        data: null
      }
    }
  },
  
  // 标记所有消息为已读
  {
    url: '/api/messages/read/all',
    method: 'patch',
    response: () => {
      messages.forEach(msg => {
        msg.read = true
      })
      
      return {
        code: 200,
        message: '所有消息已标记为已读',
        data: null
      }
    }
  },
  
  // 删除消息
  {
    url: '/api/messages/:id',
    method: 'delete',
    response: (request: any) => {
      const id = request.params?.id || request.query?.id
      if (!id) {
        return {
          code: 400,
          message: '缺少消息ID参数',
          data: null
        }
      }
      
      const index = messages.findIndex(msg => msg.id === Number(id))
      
      if (index === -1) {
        return {
          code: 404,
          message: '消息不存在',
          data: null
        }
      }
      
      const deletedMessage = messages.splice(index, 1)[0]
      
      return {
        code: 200,
        message: '删除成功',
        data: deletedMessage
      }
    }
  },
  
  // 清空所有消息
  {
    url: '/api/messages/clear',
    method: 'delete',
    response: () => {
      const count = messages.length
      messages.length = 0
      
      return {
        code: 200,
        message: `已清空${count}条消息`,
        data: null
      }
    }
  },
  
  // 获取消息设置
  {
    url: '/api/messages/settings',
    method: 'get',
    response: () => {
      return {
        code: 200,
        message: 'success',
        data: messageSettings
      }
    }
  },
  
  // 更新消息设置
  {
    url: '/api/messages/settings',
    method: 'put',
    response: (request: any) => {
      const settings = request.body
      
      messageSettings = {
        ...messageSettings,
        ...settings
      }
      
      return {
        code: 200,
        message: '设置已更新',
        data: messageSettings
      }
    }
  }
] as MockMethod[] 